<template>
  <div class="sign">
    <div class="sign-head">
      <span>账号登录</span>
    </div>

    <el-form ref="signInForm" status-icon :model="registerForm" :rules="SignInRules">
      <el-form-item prop="username">
        <el-input placeholder="用户名" v-model="registerForm.username"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" placeholder="密码" v-model="registerForm.password" @keyup.enter="handleLoginIn"></el-input>
      </el-form-item>

      <el-form-item prop="sign-btn">
        <el-button @click="handleSignUp">注册</el-button>
        <el-button type="primary" @click="handleLoginIn">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import {defineComponent,reactive,getCurrentInstance} from "vue";
import mixin from "@/mixins/mixin";
import {HttpManager} from "@/api";
import {NavName,RouterName,SignInRules} from "@/enums";


export default defineComponent({
  setup() {
    const {proxy} = getCurrentInstance();
    const {routerManager,changeIndex} = mixin();
    //登录用户名密码
    const registerForm = reactive({
      username:"",
      password:"",
    });

    async function handleLoginIn() {
      let canRun = true;
      (proxy.$refs["signInForm"] as any).validate((valid) => {
        if (!valid) return (canRun = false);
      });
      if (!canRun) return;


      try {
        const username = registerForm.username;
        const password = registerForm.password;
        const result = (await HttpManager.signIn({username,password})) as ResponseBody;
        (proxy as any).$message({
          message: result.message,
          type: result.type,
        });

        if (result.success) {
          localStorage.setItem('userId',result.data[0].id)
          proxy.$store.commit("setUserId", result.data[0].id);
          proxy.$store.commit("setUsername", result.data[0].username);
          proxy.$store.commit("setToken", true);
          changeIndex(NavName.Content);
          routerManager(RouterName.Content, { path: RouterName.Content });
          console.log(11);
        }
      } catch (error) {
        console.error(error);
      }
    }

    function handleSignUp() {
      routerManager(RouterName.SignUp,{path:RouterName.SignUp});
    }
    return {
      registerForm,
      SignInRules,
      handleLoginIn,
      handleSignUp,
    };
  },
});
</script>


<style lang="scss" scoped>
@import "@/assets/css/sign.scss";
</style>
